using Core;
using UnityEngine;

namespace g_gongjianta
{
    public abstract class Unit : Entity, IGameObject
    {
        public GameObject gameObject { get; protected set; }
        public Transform transform { get; protected set; }

        protected UnitSystem sysUnit;

        public virtual void Awake()
        {
            sysUnit = G.Get<UnitSystem>();

            gameObject = new GameObject();
            transform = gameObject.transform;

            transform.SetParent(sysUnit);
            transform.Reset();

            sysUnit.Add(this);

            IsActive = true;
        }

        public override void Dispose()
        {
            if (IsDisposed) return;

            sysUnit.Remove(this);

            base.Dispose();

            Utils.Trans.Destroy(gameObject);
        }

        /// <summary>
        /// 单位坐标点
        /// </summary>
        public virtual Vector2 Pos
        {
            get => transform.position;
            set => transform.position = value;
        }

        private Vector2 dir = Vector2.zero;

        public virtual Vector2 Dir
        {
            get => dir;
            set => dir = value;
            //if (this is Butterfly)
            //{
            //    log.err($"Dir={value}");
            //}
        }

        // public float ScaleX
        // {
        //     get => transform.localScale.x;
        //     set => transform.localScale = new Vector3(value, transform.localScale.y, transform.localScale.z);
        // }

        public bool IsActive { get; protected set; } = true;

        public virtual void SetActive(bool active)
        {
            IsActive = active;
            Utils.Trans.SetActive(gameObject, active);
        }
    }
}
